package com.skytech.component.crawler.scratch.downloader;

import org.jsoup.Jsoup;
import org.openqa.selenium.chrome.ChromeDriver;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.AbstractDownloader;
import us.codecraft.webmagic.selector.PlainText;

import java.util.concurrent.TimeUnit;

public class ChromeDownloader extends AbstractDownloader {

    private ChromeDriver chromeDriver;

    public ChromeDownloader(ChromeDriver chromeDriver){
        this.chromeDriver = chromeDriver;

    }


    @Override
    public Page download(Request request, Task task) {
        chromeDriver.get(request.getUrl());
        try {
            chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String html = chromeDriver.findElementByTagName("html").getAttribute("outerHTML");
        Page page = new Page();
        page.setRawText(html);
        page.setUrl(new PlainText(request.getUrl()));
        page.setRequest(request);
        page.setStatusCode(200);
        return page;
    }

    @Override
    public void setThread(int i) {

    }
}
